package com.example.fitnessservice.service.impl;

import com.example.fitnessservice.module.dao.courseMapper.CourseMapper;
import com.example.fitnessservice.module.dao.usermapper.UserMapper;
import com.example.fitnessservice.module.pojo.CourseDetails;
import com.example.fitnessservice.module.pojo.CourseDetailsCamp;
import com.example.fitnessservice.module.pojo.Place;
import com.example.fitnessservice.module.utils.DistanceUtil;
import com.example.fitnessservice.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/23 14:10
 */
@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)

public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseMapper mapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<CourseDetails> findOnePlaceCourse(Integer placeId, Integer type) {
        return mapper.findOnePlaceCourse(placeId, type);
    }

    @Override
    public List<CourseDetails> findNormalCourse(Integer courseType, Integer type, Integer x, Integer y) {
        //在查询场馆信息，以及场馆下的课程和训练营的时候，先更新训练营的状态
        userMapper.updateCourseStatus2();
        userMapper.updateCourseStatus3();

        //获取到所有场馆的信息
        List<Place> places = userMapper.findAllPlace();

        for (Place place : places) {
            //通过工具类计算两点之间的距离
            double distance = DistanceUtil.getDistance(x, y, place.getLongitude(), place.getLatitude(), 2);
            //将距离和场馆信息都放到map集合中
            place.setDistance(distance);
        }
        //按照距离排序
        places.sort((o1, o2) -> {
            if (o1.getDistance() > o2.getDistance()) {
                return 1;
            } else if (o1.getDistance() == o2.getDistance()) {
                return 0;
            }
            return -1;
        });
        List <CourseDetails> courses=new ArrayList<>();
        for (Place p:places){
           for (CourseDetails c:p.getCourseDetails()) {
               if(c.getCourseType().equals(courseType)&&c.getCourseStatus().equals(type)){
                   courses.add(c);
               }
           }
        }
        return courses;
    }


    @Override
    public List<CourseDetailsCamp> findCampCourse(Integer type, Integer x, Integer y) {
        //在查询场馆信息，以及场馆下的课程和训练营的时候，先更新训练营的状态
        userMapper.updateCourseStatus2();
        userMapper.updateCourseStatus3();

        //获取到所有场馆的信息
        List<Place> places = userMapper.findAllPlace();

        for (Place place : places) {
            //通过工具类计算两点之间的距离
            double distance = DistanceUtil.getDistance(x, y, place.getLongitude(), place.getLatitude(), 2);
            //将距离和场馆信息都放到map集合中
            place.setDistance(distance);
        }
        //按照距离排序
        places.sort((o1, o2) -> {
            if (o1.getDistance() > o2.getDistance()) {
                return 1;
            } else if (o1.getDistance() == o2.getDistance()) {
                return 0;
            }
            return -1;
        });
        List <CourseDetailsCamp> courses=new ArrayList<>();
        for (Place p:places){
            for (CourseDetailsCamp c:p.getCamps()) {
                if(c.getCourseStatus().equals(type)){
                    boolean flag=true;
                    if (CollectionUtils.isEmpty(courses)) {
                        courses.add(c);
                    }
                    for (CourseDetailsCamp ca : courses ){
                        if(c.getCourseId().equals(ca.getCourseId())){
                            flag = false;
                            break;
                        }
                    }
                    if(flag){
                    courses.add(c);
                    }
                }
            }
        }
        return courses;
    }
}
